<?php
/**
 *	file csf.php
 * This program is used to copy web directory file to svn directory.
 *	Make it convenient to do the svn operations.
 *
 * @version    0.1 
 * Athor tiger
 * Date 2011.03.16
 */

/*
	Store the modified time of directory or file 
	in file status.map.	
	We find the right file with ID ,not the filename,
	cause filename could be modified,cannot find the 
	right file according to the filename.
*/

error_reporting(0);

//csf home
$home = '/work/';
define('CSF_HOME',$home);

//web directory
$webDir = '/work/facebook';

//locale svn directory
$svnDir = '/wor/facebook/svn';

//difine which dir is except.
$exceptDir = array(
								$webDir.'db',
								$webDir.'doc',
								$webDir.'html',
								$webDir.'images',
								$webDir.'nbproject',
								$webDir.'ThinkPHP',
								$webDir.'uploads',
								$webDir.'web_html',
								$webDir.'esnews/Runtime',
								$webDir.'admin/Runtime',
								$webDir.'news/Runtime'								
							);

//The file which save the modify second time.
$statusList = CSF_HOME.'/status.list'; 

//store the modified file list
$status_str = '';


/**
 * Function set_status
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param 参数1
 * @return 返回值1
 */
function set_status()
{
	if(!file_exists($GLOBALS['statusList'])){
		//If not exists,create the file according to 		
		//the current web directory.Save the current
		//modified time in.
		
		//filename or directory=>modified time
		if( recur_set($GLOBALS['webDir'],1) ){
			file_put_contents($GLOBALS['statusList'],$GLOBALS['status_str']);
		}else{
			echo "Create statusList file error.";
		}					
	}	
}


/**
 * Function is_except
 * 
 * This function is to check if it is execpt dir 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param $dir
 * @return boolean
 */
function is_except($dir)
{
	if( in_array($dir,$GLOBALS['exceptDir']) ){
		return true;
	}else{
		return false;
	}	
}
/**
 * Function recur_set
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param $recur_dir $child
 * @return boolean
 */
function recur_set($recur_dir,$child)
{
	$handle = dir($recur_dir);		
	while( $entry = $handle->read() ){
		if( ($entry != ".") && ($entry != "..") ){
			if( is_dir($recur_dir."/".$entry) ){
			
                //keep the code below to finish it someday.
				// if( is_except($recur_dir) ){
				
					// echo 'Ignore dir =>'.$recur_dir."/".$entry.'</br>';
					// continue;		
				// }
				
				if( $child ){
					$GLOBALS['status_str'] .=
						$recur_dir."/".$entry."=>".modify_U($recur_dir."/".$entry)."\r\n";
					recur_set($recur_dir."/".$entry,$child);
				}
					
			}elseif( is_file($recur_dir."/".$entry) ){
					$GLOBALS['status_str'] .=  
						$recur_dir."/".$entry."=>".modify_U($recur_dir."/".$entry)."\r\n"; 	
			}			
		}				
	}
	return true;
}


/**
 * Function get_status
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param 参数1
 * @return $status_array
 */
 
function get_status()
{
	$str = file_get_contents($GLOBALS['statusList']);
	$str_arr = explode("\r\n",$str);
	// var_dump($str_arr);
	$i=0;
	$ex = array();
	$status_array=array();
	while( $i < count($str_arr)-1 ){
		$ex = explode('=>',$str_arr[$i]);
		$status_array[$ex[0]] = $ex[1]; 	
		$i++;
	}	
	return $status_array;
}

/**
 * Function file_copy
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @param $source $destination $child
 * @return boolean
 */
function file_copy($source, $destination, $child)
{
	if(!is_dir($source)){
		echo("Error:the $source is not a direction!");
		return 0;
	}
	if(!is_dir($destination)){
		mkdir($destination,0777);
	}
	$handle=dir($source);
	while($entry=$handle->read()) {
		if(($entry!=".")&&($entry!="..")){
		    if(is_dir($source."/".$entry)){
			if($child){    
					file_copy($source."/".$entry,$destination."/".$entry,$child);
				}
			}else{
					copy($source."/".$entry,$destination."/".$entry);
			}
		}
	}
    return true;
}


/**
 * Function copy_file
 * 
 * 	Check the webDir if modified,
 *	copy the modified file to svn dir.
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param 
 * @return 
 */
function copy_file($cp_dir,$child)
{

	$handle = dir($cp_dir);		
	while( $entry = $handle->read() ){
		if( ($entry != ".") && ($entry != "..") ){
			if( is_dir($cp_dir."/".$entry) ){
				if( $child ){
					copy_file($cp_dir."/".$entry,$child);
				}					
			}elseif( is_file($cp_dir."/".$entry) ){
				if( $GLOBALS['ori_sta_arr'][$cp_dir."/".$entry] != modify_U($cp_dir."/".$entry) ){
					echo "Copy : ". $cp_dir."/".$entry. "<br/>";
					$des = get_svn_path($cp_dir);
					copy($cp_dir."/".$entry,get_svn_path($cp_dir."/".$entry)); 

					//update status list.	
					$GLOBALS['ori_sta_arr'][$cp_dir."/".$entry] = modify_U($cp_dir."/".$entry);
					
				}	 
			}			
		}				
	}
	return true;
}

/**
 * Function modify_U
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param $dir
 * @return Unix second time
 */
function modify_U($dir)
{
	return date("U",filemtime($dir));	
}

/**
 * Function get_svn_path
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param $path
 * @return $svn_path
 */
function get_svn_path($path)
{
	$svn_path = 	$GLOBALS['svnDir'].substr($path,strlen($GLOBALS['webDir']));
	return $svn_path;
}

/**
 * Function update_status
 * 
 * 函数说明
 * 
 * @author TIGER
 * @date 2010-11-25
 * @copyright	 (c) 2007-2009 Orivon Team 
 * @param $new_arr
 * @return 返回值1
 */
function update_status($new_arr)
{
	$str='';	
    while ($cur = current($new_arr)) {   
        $str .= key($new_arr).'=>'.$new_arr[key($new_arr)]."\r\n";    
   	 	next($new_arr);
	}
	file_put_contents($GLOBALS['statusList'],$str);
}


//excute here ,could be change if  needs that.

set_status();
$ori_sta_arr = get_status();
copy_file($webDir,1);
update_status($ori_sta_arr);

?> 










